﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.Net.Mail;

public partial class user_processcheckout : System.Web.UI.Page
{
    public int od_id;
    string _mailServer = "smtp.gmail.com";
    int _mailPort = 587; // Work well, 465 Timeout

    string _user = "thanhnx.ltv@gmail.com";
    string _pass = "ngoxuanthanh";

    string _mailFrom = "phukieniphone@ip.vn";
    string _mailTo;
    string _firstName;
    string _lastName;


    protected void Page_Load(object sender, EventArgs e)
    {
        
        AddOrderItem();
        
        ReviewCart();
    }
    public void ReviewCart()
    {
        grvCart.DataSource = ShoppingCart.Instance.Items;
        grvCart.DataBind();
        OrderBus odBus = new OrderBus();
        string strCode = Request.QueryString["od_code"];
        DetailsViewOrder.DataSource = odBus.ReviewOrderInformation(strCode);
        DetailsViewOrder.DataBind();
        DetailsView1.DataSource = odBus.ReviewOrderInformation(strCode);
        DetailsView1.DataBind();
    }
    public void AddOrderItem()
    {

        string strID = Request.QueryString["cus_id"];
        string strCode = Request.QueryString["od_code"];
        int cus_id = Convert.ToInt32(strID);
        OrderDAL oDAL = new OrderDAL();
        DataTable dt = new DataTable();
        //gan table
        dt = oDAL.GetOdID(cus_id,strCode);
        //lay du lieu ra

        if (dt.Rows.Count > 0)
        {
            od_id = Convert.ToInt32(dt.Rows[0]["od_id"]);
            List<CartItem> list = new List<CartItem>();
            list = ShoppingCart.Instance.Items;
            foreach (CartItem cartitem in list)
            {
                oDAL.InsertOrderItem(od_id, cartitem.PdID, cartitem.Quantity, cartitem.UnitPrice);    
            }
            //goi ham send mail
            sendMail();
        }

    }
    public void sendMail()
    {
        _mailTo = Request.QueryString["OrderEmail"];
        _firstName = Request.QueryString["FirstName"];
        _lastName = Request.QueryString["LastName"];
        MailMessage mailMessage = new MailMessage();
        SmtpClient mailClient = new SmtpClient(_mailServer, _mailPort);
        mailClient.Timeout = 15000;
        mailClient.Credentials = new NetworkCredential(_user, _pass);
        mailClient.EnableSsl = true;
        //mailClient.UseDefaultCredentials = true; // no work

        mailMessage.IsBodyHtml = true;
        mailMessage.From = new MailAddress(_mailFrom);
        mailMessage.Subject = "Bill Information <phukieniphone.ip.vn>";

        // noi dung mail
        mailMessage.Body = "<h2>Hello </h2>" + _firstName + " " + _lastName + "<br><strong>Quantity: </strong>" + ShoppingCart.Instance.Items.Count + "<br><strong>Total: </strong>" + ShoppingCart.Instance.GetSubTotal();
        //
        mailMessage.To.Add(_mailTo);
        try
        {
            mailClient.Send(mailMessage);
            lblMsg.Text = "We have sent you an email and you don't reply, what are you doing ?";
        }
        catch (Exception ex)
        {
            Response.Write("Send mail fail, " + ex.Message);
        }

    }


    protected void grvCart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[3].Text = "Total: " + String.Format("{0:0.0.0} $", ShoppingCart.Instance.GetSubTotal());
        }
    }
}